package com.boot.mall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boot.mall.product.entity.BrandEntity;
import com.boot.mall.product.entity.CategoryEntity;
import com.boot.mall.product.service.BrandService;
import com.boot.mall.product.service.CategoryService;
import com.boot.mall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.boot.mall.product.entity.CategoryBrandRelationEntity;
import com.boot.mall.product.service.CategoryBrandRelationService;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author huihui
 * @email 2565036109@qq.com
 * @date 2021-05-06 14:41:26
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam("brandId") String brandId){
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brandId",brandId);
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(queryWrapper);
        return R.ok().put("page", list);
    }

    /**
     * 根据分类id查询分组关联的属性
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R getBrandsList(@RequestParam(value = "catId",required = false) String catId){
        // 查询关联分组的所有信息
        List<BrandEntity> categoryBrand =  categoryBrandRelationService.getBrandsByCatId(catId);
        // 封装BrandVo属性
        List<BrandVo> brandVos = new ArrayList<>();
        for (BrandEntity brandEntity : categoryBrand) {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            brandVos.add(brandVo);
        }
        return R.ok().put("data",brandVos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        //查询品牌信息
        BrandEntity brand = brandService.getOne(new QueryWrapper<BrandEntity>().eq("brand_id", categoryBrandRelation.getBrandId()));
        //查询分类信息
        CategoryEntity category = categoryService.getOne(new QueryWrapper<CategoryEntity>().eq("cat_id", categoryBrandRelation.getCatelogId()));
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
